package Salesman.gui;

import Salesman.Data.DataProvider;
import Salesman.Data.Icons.ImageIcon;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Frame extends JFrame {

    private TabbedPanel pane = new TabbedPanel();

    public Frame() {

        setIconImage(new ImageIcon("map.png").getImage());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setSize(1024, 768);
        centerWindow(this);
        initTabbedPanel();

        pane.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                setTitle();
            }
        });

        add("Center", pane);

        setTitle();
        setVisible(true);


    }

    private void setTitle() {
        setTitle("TSP - " + pane.getCurrentTitle());
    }

    private void initTabbedPanel() {

        pane.addButton(new Simulation(), "Simulation", new ImageIcon("compass.png"));
        pane.addButton(new CityPanel(), "Städte", new ImageIcon("map.png"));
        pane.addButton(new JScrollPane(new MatrixTable(DataProvider.getCityData(), DataProvider.getDistances())), "Matrix", new ImageIcon("docs.png"));

    }

    private void centerWindow(Window window) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = window.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        window.setLocation((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2);
    }
}
